package com.hyd.util;

import java.io.File;
import java.io.IOException;
import android.media.MediaRecorder;
import android.os.Environment;

public class RecordingUtil {
	private MediaRecorder recorder;
	public static boolean isRecord;

	public RecordingUtil() {
		
	}

	private void prepare() {
		recorder = new MediaRecorder();
		// 设置录音来源
		recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
		// 设置输出格式
		recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
		// 设置编码方式
		recorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
		// 设置采样频率
		recorder.setAudioSamplingRate(177 * 144);
		// 设置录制好的音频文件保存路径
		try {
			recorder.setOutputFile(Environment.getExternalStorageDirectory()
					.getAbsolutePath() + File.separator + "1.mp3");
			recorder.prepare();
			// 准备录制
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public void startRecording() {
		prepare();
		recorder.start();
		isRecord = true;
	}

	public void destroy() {
		if (recorder != null && isRecord == true) {
			recorder.stop();
			recorder.reset();
			recorder.release();
			recorder = null;
			isRecord = false;
		}
	}
}
